package com.example.controller;

import com.example.common.Result;
import com.example.entity.User;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *
 * 用户模块前端请求的端口
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
/*
* 新增
*
*
* */

    @PostMapping("/add")
    public Result add(@RequestBody  User user){
        userService.add(user);

     return Result.success();
    }

    /*
    * 更新
    * */
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.updateById(user);
        return Result.success();
    }

    /*
     * 删除
     * */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success();
    }

/**
 *
 * 分页查询
 */
@GetMapping("/selectPage")
public Result selectPage(User user,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "5") Integer pageSize) {
    PageInfo<User> pageInfo = userService.selectPage(user, pageNum, pageSize);
    return Result.success(pageInfo);
}

/*
* 个人资料查询
*
 */
   @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
       User user =  userService.selectById(id);
       return Result.success(user);
   }
    /*
     * 更新用户宠物ID
     * */
    @PutMapping("/updatePetssId")
    public Result updatePetssId(@RequestBody User user) {
        if (user != null ) {
            userService.updatePetssId(user.getId(), user.getPetssId());
            return Result.success();
        } else {
            return Result.error("用户不存在");
        }
    }
}